﻿namespace HuwmanCode

open System

[<RequireQualifiedAccess>]
module Seq =
    let fromGraph (accessor:'a->'a) (obj:'a) =
        let rec gen (o:'a) =
            seq { match o with
                  | null -> yield! seq []
                  | r -> yield r
                         yield! gen (accessor r)  }
        gen obj

    /// Computes the nth element in the sequence
    let nthOrDefault index (source:'t seq) =
        try 
            source |> Seq.nth index
        with _ -> Unchecked.defaultof<'t>

    /// Computes the nth element in the sequence
    let nthOrNone index (source:'t seq) =
        try 
            Some (source |> Seq.nth index)
        with _ -> None